package com.byxy.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.byxy.entity.Log;
import com.byxy.json.DataTable;
import com.byxy.service.LogService;

public class LogDataTableAction {

	private int draw;
	private int start;// 下标
	private int length;

	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	@Resource
	private LogService logService;

	public void getDataTable() {
		HttpServletResponse response = ServletActionContext.getResponse();
		// 解决中文乱码问题
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		DataTable dt = new DataTable();
		dt.setDraw(draw);
		Integer recordsTotal = (int) logService.count();
		dt.setRecordsTotal(recordsTotal);
		dt.setRecordsFiltered(recordsTotal);

		// 临界点 0,9,10
		// start=0 page=1
		// start=9 page=1
		// start=10 page=2
		int page = 1;
		if (start % length == 0) {
			page = start / length + 1;
		} else {
			page = start / length;
		}
		List<Log> data = logService.listPage(page, length);
		dt.setData(data);
		String json_str = com.alibaba.fastjson.JSON.toJSONString(dt);
		System.out.println(json_str);
		pw.write(json_str);
		pw.flush();
		pw.close();
	}

}
